<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <title>菜品类别编辑</title>

    <link rel="stylesheet" th:href="@{/layui/css/layui.css}">
    <i th:replace="~{common :: baseUrl}"></i>
</head>
<body>
<form class="layui-form" action=""  lay-filter="example">

    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">店铺名称</label>
            <div class="layui-input-inline">
                <input type="text" id="shopName" th:value="${parentFoodCategory.merchantsName}"   a class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">菜品类别</label>
            <div class="layui-input-inline">
                <input type="text" name="parentName" id="categoryName" th:value="${parentFoodCategory.parentName}" class="layui-input">
            </div>
        </div>
    </div>


    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label">菜品分类</label>
            <div class="layui-input-inline">
                <select name="id" lay-filter="levelTwo" id="levelTwo" lay-verify="required" lay-search="">
                    <option th:each="f : ${childFoodCategoryList}"  th:value="${f.id}" th:text="${f.child}"></option>
                </select>
            </div>
        </div>
    </div>
    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label">分类名称</label>
            <div class="layui-input-inline">
                <input type="text" name="child" id="twoCategoryName"  class="layui-input" th:value="${childFoodCategory.child}">
            </div>
        </div>
    </div>

    <div class="layui-form-item">

        <div class="layui-inline">
            <label class="layui-form-label">上传图片</label>
            <div class="layui-input-inline">
                <div class="layui-upload">
                    <button type="button" class="layui-btn" id="uploadPic">上传图片</button>
                    <div class="layui-upload-list">
                        <input hidden name="picPath" id="picPath" th:value="${childFoodCategory.picPath}">
                        <img width="100px" height="100px" class="layui-upload-img" name="showPic" id="showPic" th:src="${childFoodCategory.picPath}">
                        <p id="uploadText"></p>
                    </div>
                </div>
            </div>
        </div>

    </div>

    <div class="layui-form-item">
        <label class="layui-form-label">状态</label>
        <div class="layui-input-block">
            <input type="radio" name="status" value="1" title="已启用" th:checked="${childFoodCategory.status == '1'}" >
            <input type="radio" name="status" value="0" title="未启用" th:checked="${childFoodCategory.status == '0'}" >
        </div>
    </div>

    <div class="layui-form-item">
        <div class="layui-input-block">
            <button type="submit" class="layui-btn" lay-submit="" lay-filter="sub">立即提交</button>
            <button type="reset" class="layui-btn layui-btn-primary">重置</button>
        </div>
    </div>

</form>



</body>
<script th:src="@{/layui/layui.js}"></script>
<script th:inline="none">
    //JavaScript代码区域
    layui.use(['element','form','upload','layer'], function() {
        var element = layui.element;
        var form = layui.form;
        var upload = layui.upload;
        var layer = layui.layer;
        var $ = layui.$;




        //图片上传
        var uploadInst = upload.render({
            elem: '#uploadPic'
            ,url: baseUrl+'upload/pic'
            ,before: function(obj){
                //预读本地文件示例，不支持ie8
                obj.preview(function(index, file, result){
                    $('#showPic').attr('src', result); //图片链接（base64）
                });
            }
            ,done: function(res){
                //如果上传失败
                if(res.code != 0){
                    return layer.msg('上传失败');
                }else {
                    $("#picPath").val(res.data.src)
                }
                //上传成功
            }
            ,error: function(){
                //演示失败状态，并实现重传
                var demoText = $('#uploadText');
                demoText.html('<span style="color: #FF5722;">上传失败</span> <a class="layui-btn layui-btn-xs demo-reload">重试</a>');
                demoText.find('.demo-reload').on('click', function(){
                    uploadInst.upload();
                });
            }
        });


        //查询不同的二级分类（动态赋值）
        form.on('select(levelTwo)', function(data){

            $.getJSON(
                baseUrl+"food/category/edit-list",
                {id:data.value},
                function (result) {
                    if (result.code==0){

                        var foodcategory = result.data;


                            $("#showPic").attr("src",foodcategory.picPath)
                        $("#picPath").val(foodcategory.picPath)

                            //表单赋值
                            form.val('example', {
                                "child": foodcategory.child
                                ,"status": foodcategory.status

                            });



                    }else {
                        layer.msg(result.msg);
                    }
                }
            )

        });

        //提交
        form.on('submit(sub)', function(data){

            //发送ajax请求  编辑APP信息
            $.ajax({
                url:baseUrl+"food/category/edit",
                data:data.field,
                type:"post",
                dataType:"json",
                success:function (result) {
                    if (result.code==0){
                        layer.open({
                            title: '编辑菜品分类信息'
                            ,content: '恭喜你! 编辑菜品分类信息成功！'
                            ,end:function () {
                                //当你在iframe页面关闭自身时
                                var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                parent.layer.close(index); //再执行关闭
                            }
                        });
                    }else {
                        layer.open({
                            title: '编辑菜品分类信息'
                            ,content: result.msg
                        });
                    }
                },
                error:function () {
                    layer.msg("服务器原地爆炸,请联系管理员!!!");
                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。

        });





    });

</script>
</html>